import { navigateTo, reLaunch, showToast } from '@tarojs/taro';
import { useAuthStore } from '@/store';
import { useRouterPush } from '@/composables';
import { storeToRefs } from 'pinia';

export function toHelp() {
  navigateTo({
    url: '/package/help/index'
  });
}
export function toMine(type = 0) {
  const { nurseUser, openid } = storeToRefs(useAuthStore());

  const { resetAuthStore } = useAuthStore();

  if (nurseUser?.value?.id && openid?.value) {
    navigateTo({
      url: `/package/mine/index?type=${type}`
    });
  } else {
    showToast({
      title: '请先登录！',
      icon: 'none',
      duration: 1000
    });
    setTimeout(() => {
      const { toLogin } = useRouterPush();
      resetAuthStore();
      toLogin();
    }, 1000);
  }
}
export function toGuardian(id = '') {
  const { nurseUser, openid } = storeToRefs(useAuthStore());
  const { resetAuthStore } = useAuthStore();
  if (nurseUser.value.id && openid.value) {
    navigateTo({
      url: `/package/guardian/index?id=${id}`
    });
  } else {
    showToast({
      title: '请先登录！',
      icon: 'none',
      duration: 1000
    });
    setTimeout(() => {
      const { toLogin } = useRouterPush();
      resetAuthStore();
      toLogin();
    }, 1000);
  }
}

export function toAddr(id = '', home = false) {
  const { nurseUser, openid } = storeToRefs(useAuthStore());
  const { resetAuthStore } = useAuthStore();
  if (nurseUser.value.id && openid.value) {
    navigateTo({
      url: home ? `/package/addr/home?id=${id}` : `/package/addr/index?id=${id}`
    });
  } else {
    showToast({
      title: '请先登录！',
      icon: 'none',
      duration: 1000
    });
    setTimeout(() => {
      const { toLogin } = useRouterPush();
      resetAuthStore();
      toLogin();
    }, 1000);
  }
}

export function toNursesDetail(id: string) {
  navigateTo({
    url: `/package/nursesdetail/index?id=${id}`
  });
}

export function toGoodsDetail(param: {
  id: string;
  pic?: string;
  pname?: string;
  unit?: string;
  nurseType?: string;
  goods_type?: string;
  nurseTypeText?: string;
}) {
  const str = Array.from(new Map(Object.entries(param)))
    .map(([key, value]) => `${key}=${value}`)
    .join('&');

  navigateTo({
    url: `/package/goodsdetail/index?${str}`
  });
  // navigateTo({
  //     url: `/package/goodsdetail/index?id=${id}&pname=${pname}&pic=${pic}&unit=${unit}&nurseType=${nurseType}`
  // });
}

export function toGoodsPay(param: {
  goodsId?: string;
  pid?: string;
  pname?: string;
  num?: number;
  name?: string;
  pic?: string;
  price?: number;
  unit?: string;
  nurseType?: string;
  nurseTypeText?: string;
  orderId?: string;
  status?: string;
  pay?: string;
}) {
  const str = Array.from(new Map(Object.entries(param)))
    .map(([key, value]) => `${key}=${value}`)
    .join('&');

  navigateTo({
    url: `/package/goodspay/server?${str}`
  });
}

/** 跳转登录页 */
export function toLogin() {
  reLaunch({
    url: '/package-other/login/login'
  });
}

export function toComment(orderId: string) {
  navigateTo({
    url: `/package/comment/index?orderId=${orderId}`
  });
}

export function toRefund(orderId: string) {
  navigateTo({
    url: `/package/refund/index?orderId=${orderId}`
  });
}

export function toBill(orderId: string, amt: number, type = 1) {
  navigateTo({
    url: `/package/bill/index?orderId=${orderId}&amt=${amt}&type=${type}`
  });
}

export function toBillExtend(extend: string) {
  navigateTo({
    url: `/package/bill/extend?extend=${extend}`
  });
}

export function toNearShop() {
  navigateTo({
    url: `/package/nearshop/index`
  });
}

export function toSwitchCity() {
  navigateTo({
    url: `/package/switchcity/index`
  });
}
